package com.unitd.frame.sso.common.helper;

import com.unitd.frame.comm.utils.StringUtils;
import com.unitd.frame.sso.common.encrypt.AlgorithmEnum;
import com.unitd.frame.sso.common.token.SSOToken;
import com.unitd.frame.sso.common.token.Token;
import com.unitd.frame.sso.exception.SsoException;
import com.unitd.frame.sso.service.auth.ISSOAuthorization;
import com.unitd.frame.sso.service.auth.impl.DefSSOAuthorization;
import com.unitd.frame.sso.service.cache.ISSOCache;
import com.unitd.frame.sso.service.encrypt.ISSOEncrypt;
import com.unitd.frame.sso.service.encrypt.impl.MySSOEncrypt;
import com.unitd.frame.sso.service.parser.ISSOParser;
import com.unitd.frame.sso.service.parser.impl.FastJsonParser;
import com.unitd.frame.sso.service.statistic.ISSOStatistic;

/**
 * @desc SSO 反射辅助类
 * @filename SSOReflectHelper.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016-10-16
 */
public class SSOReflectHelper {

	private static ISSOEncrypt encrypt = null; 							// SSO 对称加密接口
	private static ISSOParser parser = null; 							// SSO Token 解析接口
	private static ISSOCache cache = null; 								// SSO 缓存接口
	private static ISSOAuthorization authorization = null; 				// SSO 权限授权接口
	private static ISSOStatistic statistic = null; 						// SSO 在线人数统计接口

	/**
	 * @desc 反射获取自定义Token
	 * @param tokenClass 自定义的token类
	 * @return {@link Token}
	 */
	public static Token getConfigToken(String tokenClass) {
		Token token = null;
		/* 判断是否自定义 Token 默认 SSOToken */
		if(StringUtils.isBlank(tokenClass)) {
			token = new SSOToken();
		} else {
			try {
				Class<?> tc = Class.forName(tokenClass);
				if (tc.newInstance() instanceof Token) {
					token = (Token) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException(e);
			}
		}
		return token;
	}

	/**
	 * @desc 反射获取自定义对称加密接口实现类Encrypt
	 * @param encryptClass 自定义的加解密工具类
	 * @param encryptAlgorithm 自定义的加解密算法类型
	 * @return {@link ISSOEncrypt}
	 */
	public static ISSOEncrypt getConfigEncrypt(String encryptClass, String encryptAlgorithm) {

		if (encrypt != null) {
			return encrypt;
		}

		/* 判断是否自定义 Encrypt 默认 RC4 */
		if(StringUtils.isBlank(encryptClass)) {
			encrypt = new MySSOEncrypt(AlgorithmEnum.convert(encryptAlgorithm));
		} else {
			try {
				Class<?> tc = Class.forName(encryptClass);
				if (tc.newInstance() instanceof ISSOEncrypt) {
					encrypt = (ISSOEncrypt) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException(e);
			}
		}
		return encrypt;
	}

	public static void setConfigEncrypt(ISSOEncrypt configEncrypt) {
		encrypt = configEncrypt;
	}

	/**
	 * @desc 反射获取自定义SSOParser
	 * @param parserClass 自定义的parser类
	 * @return {@link ISSOParser}
	 */
	public static ISSOParser getConfigParser(String parserClass) {

		if (parser != null) {
			return parser;
		}

		/* 获取自定义 SSOParser */
		if(StringUtils.isBlank(parserClass)) {
			parser = new FastJsonParser();
		} else {
			try {
				Class<?> tc = Class.forName(parserClass);
				if (tc.newInstance() instanceof ISSOParser) {
					parser = (ISSOParser) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException("SSO的属性配置文件sso.properties中没有找到属性名为sso.parser.class的属性或者没有ISSOParser接口的实现类!", e);
			}
		}
		return parser;
	}

	public static void setConfigParser(ISSOParser configParser) {
		parser = configParser;
	}

	/**
	 * @desc 反射获取自定义SSOCache
	 * @param cacheClass 自定义的SSO缓存处理类
	 * @return {@link ISSOCache}
	 */
	public static ISSOCache getConfigCache(String cacheClass) {

		if (cache != null) {
			return cache;
		}

		/* 反射获得缓存类 */
		if(StringUtils.isNotBlank(cacheClass)) {
			try {
				Class<?> tc = Class.forName(cacheClass);
				if (tc.newInstance() instanceof ISSOCache) {
					cache = (ISSOCache) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException(e);
			}
		}
		return cache;
	}

	public static void setConfigCache(ISSOCache configcache) {
		cache = configcache;
	}

	/**
	 * @desc 反射获取自定义SSOAuthorization
	 * @param authorizationClass 自定义 SSO 权限授权接口实现类
	 * @return {@link ISSOAuthorization}
	 */
	public static ISSOAuthorization getAuthorization(String authorizationClass) {
		if (authorization != null) {
			return authorization;
		}

		/* 反射获得权限类 */
		if(StringUtils.isNotBlank(authorizationClass)) {
			try {
				Class<?> tc = Class.forName(authorizationClass);
				if (tc.newInstance() instanceof ISSOAuthorization) {
					authorization = (ISSOAuthorization) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException(e);
			}
		} else {
			authorization = new DefSSOAuthorization();
		}
		return authorization;
	}

	public static void setAuthorization(ISSOAuthorization authorization) {
		SSOReflectHelper.authorization = authorization;
	}

	/**
	 * @desc 反射获取自定义SSOStatistic
	 * @param statisticClass 自定义的sso信息统计类
	 * @return {@link ISSOStatistic}
	 */
	public static ISSOStatistic getConfigStatistic(String statisticClass) {

		if (statistic != null) {
			return statistic;
		}

		/* 反射获得统计类  */
		if(StringUtils.isNotBlank(statisticClass)) {
			try {
				Class<?> tc = Class.forName(statisticClass);
				if (tc.newInstance() instanceof ISSOStatistic) {
					statistic = (ISSOStatistic) tc.newInstance();
				}
			} catch (Exception e) {
				throw new SsoException(e);
			}
		}
		return statistic;
	}

	public static void setConfigStatistic(ISSOStatistic configStatistic) {
		statistic = configStatistic;
	}
}